import datetime,sys
import pandas as pd
import numpy as np
import requests, json
import base64
import warnings
warnings.filterwarnings('ignore')
def download(url):
    req = requests.get(url)
    filename = url.split('/')[-1]
    if req.status_code != 200:
        print('下载异常')
        return
    try:
        with open(filename, 'wb') as f:
            # req.content为获取html的内容
            f.write(req.content)
            print('下载成功')
    except Exception as e:
        print(e)

    start = datetime.datetime.now()
    p_day_1 = (start - datetime.timedelta(days=1)).strftime("%Y%m%d")
    xls_name = p_day_1 + '_U504.xls'
    df = pd.read_excel(xls_name)
    # df = XDataFrameUtils.excel2dataframe(p_excel_file_path=xls_name)
    df.rename(columns={'业务链路ID': 'xbzti'}, inplace=True)
    df.rename(columns={'机组号': 'unitno'}, inplace=True)
    df.rename(columns={'消息号': 'messageunitno'}, inplace=True)
    df.rename(columns={'材料号': 'matno'}, inplace=True)
    df.rename(columns={'生产结束时间': 'proetime'}, inplace=True)
    df.rename(columns={'执行状态': 'exctstatus'}, inplace=True)
    df.rename(columns={'时效状态': 'timestatus'}, inplace=True)
    df.rename(columns={'是否关键消息号': 'keyflag'}, inplace=True)
    df.rename(columns={'总耗时': 'totaltime'}, inplace=True)
    df.rename(columns={'说明': 'failedreason'}, inplace=True)
    df.rename(columns={'失败步骤分组': 'stepgroup'}, inplace=True)
    df.rename(columns={'失败步骤所属部门': 'dept'}, inplace=True)
    df.rename(columns={'失败步骤联系人': 'stepconnmessage'}, inplace=True)
    df.rename(columns={'录入时间': 'inputtime'}, inplace=True)
    df.rename(columns={'更新时间': 'updatetime'}, inplace=True)
    now = datetime.datetime.now()
    now_1 = now.strftime('%Y%m%d%H%M%S')
    df['rec_create_time'] = now_1
    df['matno'] = df['matno'].astype(str)
    df['totaltime'] = df['totaltime'].astype(str)
    df['rec_create_time'] = df['rec_create_time'].astype(str)
    records = dict()
    records['columns'] = df.columns.values.tolist()
    records['values'] = df.values.tolist()
    b = str(records).replace("'", "\"")
    print(b)
    records = b
    bytedatas = records.encode('UTF-8')
    # bytedatas = records
    url = 'http://10.70.248.187:80/stsrest/api/record/bgtasszzqm/t_dwd_fact_zzqm_data_monitor'
    user = 'admin@bgtas4q'  # 用户名
    passwd = 'adminDFb3h'  # 密码

    serect = user + ":" + passwd
    serect = str(base64.b64encode(serect.encode("utf-8")), "utf-8")

    headers = {'Content-Type': 'application/json',
               'charset': 'utf-8',
               'Authorization': 'Basic {}'.format(serect)}
    try:
        print(headers)
        r = requests.post(url=url, data=bytedatas, headers=headers, timeout=5)
        print(111)
        d = json.loads(r.text)
        print(d)
    except Exception as e:
        print(str(e), file=sys.stderr)

    return 0




if __name__ == '__main__':
    start = datetime.datetime.now()
    p_day_1 = (start - datetime.timedelta(days=1)).strftime("%Y%m%d")
    url = 'http://eplat.baogang.info/node/resource/cemc/' + p_day_1 + '_U504.xls'
    # url = 'http://eplat.baogang.info/node/resource/cemc/20220918_U504.xls'  # xxx为url，根据自己需求输入
    download(url)
